//app.js
// wxa8af6e94d82b192f wx7b056c02f1df7d8e
App({
  param: {
    Appid: "wx7b056c02f1df7d8e",
    scanCallCoupon: null,
    address: null
  },
  keys: {
    USER_INFO: "USER_INFO",
    INTERFACE_TOKEN: "INTERFACE_TOKEN",
    IS_RELOGIN: false,
    CustomerId: "CustoemrId"
  },
  onLaunch: function () {
    //this.checkSession(null);
  },
  
  /**
  * 设置key
  */
  setStorage: function (k, v) {
    wx.setStorageSync(k, v);
  },
  /**
   * 移除key
   */
  removeStorage: function (k) {
    wx.removeStorage({ key: k });
  },
  /**
   * 读取key
   */
  getStorage: function (k) {
    return wx.getStorageSync(k);
  },
  getCurrentPageUrl(){
    var pages = getCurrentPages()    //获取加载的页面
    var currentPage = pages[pages.length - 1]    //获取当前页面的对象
    return currentPage.route.toLowerCase()    //当前页面url
  },
  //得到CustomerId
  getCustomerId: function () {
    var that = this;
    return wx.getStorageSync(that.keys.CustomerId);
  },
  //得到随机串
  getRandomChar(num) {
    var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
    var maxPos = chars.length
    var str = ""
    for (var i = 0; i < num; i++) {
      str += chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return str
  },
  //不为空
  notNull: function (obj, defaultobj) {
    if (obj == null || obj == '')
      return defaultobj;
    return obj;
  },
  trim: function(value){
    return value.replace(/(^\s+)|(\s+$)/g, "");
  },
  showNotify: function (that, msg, callback){
    that.setData({ msgprop: msg });
    setTimeout(function () {
      that.setData({ msgprop: "" });
      if (callback){
        callback();
      }
    }, 1500);
  },
  editTime:function(obj){//修改时间
    let arr1=obj.split(' ');
    let arr2=arr1[1].substring(0,5);
    obj=arr1[0]+'  '+arr2;
    // obj1=arr1[0];
    // obj2=arr2;
    return obj;
    // return obj1;
    // return obj2;
  },
  //异常消息显示
  showError: function (obj) {
    wx.showToast({
      title: obj.msg,
      image:"../../images/warning.png",
      duration:2000
    })
  },
  //正确消息显示
  showSuccess: function (obj) {
    wx.showToast({
      title: obj.msg,
      duration: 2000
    })
  },
  //通知
  alert: function (obj) {
    wx.showModal({
      content: obj.msg,
      showCancel: obj.showCancel,
      success: obj.s
    })
  },
  //加载中..
  showLoading: function (obj) {
    obj = this.notNull(obj, {})
    if (obj.title == null)
      obj.title = '加载中...'
    if (obj.mask == null)
      obj.mask = true
    wx.showLoading({ title: obj.title, mask: obj.mask })
  },
  //验证
  validate: function (v, t, l) {
    if (v == null)
      return false
    var b = false
    if (t == 'notEmpty'){
      b = v.trim().length >= 1 ? true : false
      }
    else if (t == 'number'){
      b = /^\d+$/.test(v)
    }else if (t == 'phone'){
      b = /0?(13|14|15|18|17)[0-9]{9}/.test(v)
    }else if(t=='password'){ 
      b=/((?=[^\u4e00-\u9fa5]*\d)(?=[^\u4e00-\u9fa5]*\D)|(?=[^\u4e00-\u9fa5]*[a-zA-Z])(?=[^\u4e00-\u9fa5]*[^a-zA-Z]))[^\u4e00-\u9fa5]*$/.test(v)
    } else if (t == 'email'){
        b = /.*@[A-Za-z0-9]+\.[A-Za-z]{2,14}/.test(v)
    }else if(t=='leim'){
          b=/^[a-zA-Z0-9\u4e00-\u9fa5]+$/.test(v)
    }
    if (l != null) {
      b = v.trim().length >= l ? true : false
    }
    return b
  },
  globalData: {
    hasLogin: false
  }
})